import re
from typing import List

from pydantic import BaseModel, Field, validator, root_validator


class RegisterTenantModel(BaseModel):

    name: str = Field(..., description="租户名字", max_length=64)
    slug: str = Field(..., description="租户的二级域名", max_length=16, regex="^[a-z]+$")
    domain: str = Field(None, description="租户的域名,http/https开头")
    icon: str = Field(..., description="租户的icon", max_length=255)
    title: str = Field(None, description="租户的title", max_length=32)
    invited_code: str = Field(None, description="租户的受邀码")
    currency: str = Field(..., description="币种")
    language: str = Field(..., description="语言")

    @validator("domain")
    def validate_domain(cls, value: str):
        if value.startswith("http://") or value.startswith("https://"):
            return value
        raise ValueError("domain必须是以http或者https开头")

    @validator("slug")
    def validate_slug(cls, value: str):
        if not re.match("^[a-z]+$", value):
            raise ValueError("slug只能由小写字母组成")
        return value


class UpdateTenantModel(BaseModel):
    name: str = Field(None, description="租户名字", max_length=64)
    icon: str = Field(None, description="租户的icon", max_length=255)
    title: str = Field(None, description="租户的title", max_length=32)

    @root_validator()
    def validate_root(cls, attrs):
        """
        name icon title必须更新一个
        :param attrs:
        :return:
        """
        if len(attrs.keys()) == 0:
            raise ValueError("缺失更新数据")
        return attrs


class UpdateTenantLanguageModel(BaseModel):
    language_code: str = Field(..., description="语言code")
    enable: bool = Field(..., description="是否启用 True表示启用 False表示禁用")


class UpdateTenantCurrencyModel(BaseModel):
    currency_code: str = Field(..., description="币种code")
    enable: bool = Field(..., description="是否启用 True表示启用 False表示禁用")


if __name__ == '__main__':
    RegisterTenantModel(name="huang", slug="huang", icon="http://111.png", domain="http://")
